﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace Fak {

	public partial class FormInherit : Form {

		public FormInherit() {
			this.StartPosition = FormStartPosition.CenterScreen;
			InitializeComponent();
		}

		private bool NumberEntered;

		private bool CheckIfNumericKey( Keys K, bool isDecimalPoint ) {
			if ( K == Keys.Back ) //backspace?
				return true;

			else if ( K == Keys.OemPeriod || K == Keys.Decimal )  //decimal point?
				return isDecimalPoint ? false : true;       //or: return !isDecimalPoint
			else if ( ( K >= Keys.D0 ) && ( K <= Keys.D9 ) )      //digit from top of keyboard?
				return true;
			else if ( ( K >= Keys.NumPad0 ) && ( K <= Keys.NumPad9 ) )    //digit from keypad?
				return true;
			else
				return false;   //no "numeric" key
		}

		private void TxtBox_KeyDown( object sender, KeyEventArgs e ) {
			if ( RetTextEmpty( ( ( TextBox )sender ).Tag.ToString() ).Contains( "N" ) ) {
				NumberEntered = CheckIfNumericKey( e.KeyCode, false );
			}
		}

		private void TxtBox_KeyPress( object sender, KeyPressEventArgs e ) {
			if ( ( ( TextBox )sender ).Text.Length < 0 ) {
				if ( RetTextEmpty( ( ( TextBox )sender ).Tag.ToString() ).Contains( "N" ) ) {
					if ( NumberEntered == false ) {
						e.Handled = true;
					}
				}
			}
		}

		private void FormInherit_Load( object sender, EventArgs e ) {
			this.KeyPreview = true;

			foreach ( DataGridView grid in GetControls( this ).OfType<DataGridView>() ) {
				grid.TabStop = false;
			}

			foreach ( Control TxtBox in GetControls( this ).OfType<TextBox>() ) {
				string tag = "";
				try {
					tag = TxtBox.Tag.ToString();
				}
				catch ( Exception ex ) {
					tag = "_";
					continue;
				}

				if ( TxtBox is TextBox ) {
					TxtBox.Enter += new EventHandler( txtEnter );
					TxtBox.Leave += new EventHandler( txtLeave );
					TxtBox.KeyDown += new KeyEventHandler( TxtBox_KeyDown );
					TxtBox.KeyPress += new KeyPressEventHandler( TxtBox_KeyPress );
				}
			}
		}

		private void FormInherit_KeyDown( object sender, KeyEventArgs e ) {
			if ( e.KeyCode == Keys.Escape )
				this.Close();
		}

		public static Control[] GetControls( Control findIn ) {
			List<Control> allControls = new List<Control>();

			foreach ( Control oneControl in findIn.Controls ) {
				allControls.Add( oneControl );
				if ( oneControl.Controls.Count > 0 )
					allControls.AddRange( GetControls( oneControl ) );
			}
			return allControls.ToArray();
		}

		private void txtEnter( object sender, EventArgs e ) {
			( ( TextBox )sender ).BackColor = Color.LightGreen;
		}

		public String RetTextEmpty( String txt ) {
			if ( string.IsNullOrEmpty( txt ) ) {
				return " ";
			} else {
				return txt;
			}
		}

		private void txtLeave( object sender, EventArgs e ) {
			if ( ( ( TextBox )sender ).Text.Length < 1 && RetTextEmpty( ( ( TextBox )sender ).Tag.ToString() ).Contains( "*" ) ) {
				( ( TextBox )sender ).BackColor = Color.Red;
			} else {
				( ( TextBox )sender ).BackColor = Color.Empty;
			}
		}
	}
}